home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / NEWSTAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  5.8 KB  |  238 lines

  1. /* Star simulation taken from Computer Shopper March 1993
  2.    Ported from ibm to Atari ST :    March 16,1993
  3.                                     Jeff Bilger   Texas A&M University
  4.                                     jmb8302@neuron.tamu.edu
  5. */
  6.  
  7. #include <linea.h>
  8. #include <osbind.h>
  9.  
  10. /*random number generator */
  11.  
  12. #define random(x) abs(Random()%(x))
  13.  
  14. /*returns a number from 0 to (x-1) */
  15.  
  16. #define MAX_STARS 100
  17. #define SCALE 10  
  18. #define CONSOLE 2
  19. #define xmed 639 
  20. #define ymed 199
  21. #define xlow 319
  22. #define ylow 199
  23. #define WHITE 0 
  24. #define BLACK 3 
  25. #define GREEN 1
  26. #define RED   2
  27. #define BLUE  4
  28.  
  29.  
  30. typedef struct
  31.  {
  32.   int  x,y;
  33.  }POINT;
  34.  
  35. typedef struct
  36.  {
  37.   int speed;
  38.   POINT loc;       /* stars location */
  39.   POINT dir;       /* its direction */
  40.   int   size;      /* 1x1 or 2x2 pixel */
  41.   int   color;      /* its color  */
  42.  }STAR;
  43.  
  44. struct
  45.  {
  46.    STAR stars[MAX_STARS];           /* array of stars */
  47.   int  numstars;                   /* # of active stars */
  48.   POINT screen;                    /* screen size */
  49.   int   speed;                     /* speed as a percentage */
  50.  }g;
  51.  
  52.  
  53.  
  54.  
  55.  
  56. main()
  57. {
  58.  lineaport *myport;
  59.  char c;
  60.  int rez,xrez,yrez;
  61.  int pl0,pl1,pl2,pl3,
  62.      col0,col1,col2,col3,
  63.      exit=0;
  64. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  65.  
  66.   myport = a_init();
  67.   a_hidemouse();
  68. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  69.  
  70. col0=Setcolor(0,-1);             /* makes border black */
  71. col1=Setcolor(3,-1);             /* color of pixels. text */
  72. col2=Setcolor(2,-1);             /* 2= background */
  73. col3=Setcolor(1,-1);    
  74.  
  75.  
  76.    pl0=myport->plane0 ;       /* MED RES, 2 PLANES. LO RES MAKE 4 PLANES */
  77.    pl1=myport->plane1 ;       
  78.    pl2=myport->plane2 ;
  79.    pl3=myport->plane3 ;
  80.  
  81.  
  82.    myport->plane0 = 0;       /* MED RES, 2 PLANES. LO RES MAKE 4 PLANES */
  83.    myport->plane1 = 0;       
  84.    myport->plane2 = 0;
  85.    myport->plane3 = 0;
  86.  
  87. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  88.     rez = Getrez();            /* 0 low,1 med 2 hi */
  89.      if ( rez == 2) {puts("Hi rez not supported.\n"); exit=1;}
  90.       if (rez == 1) {xrez = xmed; yrez=ymed;}
  91.        if (rez == 0) { xrez =xlow; yrez=ylow;}
  92.                                    /* 0= border and all printed text
  93.                                      including aputpixels */
  94. Setcolor(0,0x000);             /* makes border black */
  95. Setcolor(3,0x777);             /* color of pixels. text */
  96. Setcolor(2,0x222);             /* 2= background */
  97. Setcolor(1,0x444);    
  98.     
  99.  
  100.      puts("Starfield simulation Keys: + Faster    - Slower    X exit");
  101.       g.screen.x = xrez * SCALE;        /* set up x,y max resolution */
  102.       g.screen.y = yrez  * SCALE;
  103.       g.speed = 10;
  104.  
  105.        g.numstars = 0;
  106.     while(!exit)
  107.       {
  108.        while ( !(Bconstat(CONSOLE)))       /* main loop */
  109.         {
  110.           if( g.numstars < MAX_STARS)
  111.            {
  112.             NewStar(&g.stars[g.numstars]);
  113.             g.numstars++;
  114.            }
  115.           MoveStars();
  116.          }
  117.        switch( (c=Bconin(CONSOLE)))
  118.           {
  119.            case '+': if(g.speed < 300) g.speed++; break;
  120.            case '-': if(g.speed > 1  ) g.speed--; break;
  121.            case 'x':
  122.            case 'X':Setcolor(0,col0);             /* makes border black */
  123. Setcolor(3,col1);             /* color of pixels. text */
  124. Setcolor(2,col2);             /* 2= background */
  125. Setcolor(1,col3);    
  126.  
  127.                     exit=1;
  128.           }
  129.  } /*end of while(1) */
  130.  
  131.    myport->plane0 =pl0;       /* MED RES, 2 PLANES. LO RES MAKE 4 PLANES */
  132.    myport->plane1 =pl1;       
  133.    myport->plane2 =pl2;
  134.    myport->plane3 =pl3;
  135.  
  136.  
  137. }
  138. /***********************************************/
  139. EraseStar( star ,color)
  140. STAR *star;
  141. int  color;
  142. {
  143.  int x,y;
  144.  
  145.  
  146.  x = star->loc.x/SCALE;             
  147.  y = star->loc.y/SCALE;
  148.  
  149.  if( y < 186 || y > 193)
  150.   a_putpixel(x,y,color);
  151.  
  152.   
  153. }
  154.  
  155. /************************************************/
  156. /***********************************************/
  157. DrawStar( star )
  158. STAR *star;
  159.  
  160. {
  161.  int x,y;
  162.  
  163.  
  164.  x = star->loc.x/SCALE;             
  165.  y = star->loc.y/SCALE;
  166.  if( x < 220 ) star->speed++;
  167.  if( x > 420 ) star->speed++; 
  168.  if( y < 60  ) star->speed++; 
  169.  if( y > 130 ) star->speed++; 
  170.  
  171.   if( x < 220 && x > 150) star->color=1;
  172.   if( x > 420 && x < 530) star->color=1;
  173.   if( y < 60 && y > 30) star->color=1;
  174.   if( y > 130 && y < 160) star->color=1;
  175.   if( x < 151 || x > 529){ star->speed+=2; star->color = BLACK;}
  176.   if( y < 31  || y > 159){ star->speed+=2; star->color = BLACK;}
  177.  
  178.   if( y < 186 || y > 193)
  179.     a_putpixel(x,y,star->color);
  180.  
  181.   
  182. }
  183.  
  184. /************************************************/
  185. NewStar( star )
  186. STAR *star;
  187. {
  188.  int ran,ran1;
  189.  
  190.  star->speed = 0;
  191.  
  192.  star->loc.x = (g.screen.x / 2)+ random(SCALE*2);
  193.  star->loc.y = (g.screen.y / 2)+ random(SCALE*2);
  194.         /* place star at center of screen */
  195.  
  196.    do
  197.    {
  198.     star->dir.x = SCALE - random(SCALE*2);  /* calc dir */
  199.     star->dir.y = SCALE - random(SCALE*2);
  200.      }while(star->dir.x == 0 || star->dir.y == 0);  /* if 0,0 keep trying*/
  201.  
  202.     switch(random(40))
  203.       {
  204.        case 39: star->color = 2; break;
  205.        case 38: star->color = 2; break;
  206.        case 37: star->color  =2; break;
  207.        default: star->color = 2;
  208.       }
  209. }
  210. /***********************************************************/
  211. MoveStars()
  212. {
  213.  int i;
  214.  STAR *star;
  215.  
  216.  for(star = &g.stars[g.numstars-1]; star >= g.stars; star--)
  217.   {
  218.    EraseStar(star,0);  /* erase it */
  219.    star->loc.x += star->dir.x * (g.speed+star->speed)/3;
  220.    star->loc.y += star->dir.y * (g.speed+star->speed)/3;
  221.        if( star->loc.x < 0 || star->loc.x >= g.screen.x || star->loc.y <0
  222.         || star->loc.y >= g.screen.y)
  223.          {
  224.           NewStar( star );
  225.          }
  226.         else
  227.          {
  228.           DrawStar(star);        
  229.          }
  230.        }
  231.      }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.